﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DQYT.HTDZ.BLL;
using DQYT.HTDZ.Model;

namespace DQYT.HTDZ.Web.Views.SystemManage
{
    public partial class Role : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            #region
            try
            {
                lblMsg.Text = "";
                BDMenuBLL menuBLL = new BDMenuBLL();
                lblPath.Text = menuBLL.GetTitlePath(Request["mnu_id"]);
                if (!IsPostBack)
                {
                    if (Request["action"] == "add")
                    {
                        btnEdit.Visible = false;
                        InitData(false);
                    }
                    if (Request["action"] == "update")
                    {
                        btnAdd.Visible = false;
                        InitData(true);
                    }
                }
            }
            catch (Exception ex)
            {
                lblMsg.Text = "错误信息：" + ex.Message;
            }
            #endregion
        }

        /// <summary>
        /// 初始化数据
        /// </summary>
        /// <param name="isEdit">是否是编辑</param>
        private void InitData(bool isEdit)
        {
            #region

            //初始化基本信息
            if (isEdit)
            {
                BDRoleBLL dJiaoSe = new BDRoleBLL();
                BDRoleModel mJiaoSe = new BDRoleModel();
                mJiaoSe = dJiaoSe.GetModel(Convert.ToInt32(Request["id"].ToString()));
                txtJSE_NAME.Text = mJiaoSe.JSE_NAME;
                txtJSE_MS.Text = mJiaoSe.JSE_DESCRIPTION;
            }

            #endregion
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            #region
            try
            {
                BDRoleBLL dJiaoSe = new BDRoleBLL();
                string strJSE_NAME = txtJSE_NAME.Text.Trim();
                if (strJSE_NAME == string.Empty)
                {
                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('请填写角色名称')", true);
                    return;
                }
                else
                {
                    bool isCZ = dJiaoSe.CheckMC(strJSE_NAME);
                    if (isCZ)
                    {
                        
                        return;
                    }
                }

                BDRoleModel mJiaoSe = new BDRoleModel();

                mJiaoSe.JSE_ID = dJiaoSe.GetNextID();
                mJiaoSe.JSE_NAME = txtJSE_NAME.Text.Trim();
                mJiaoSe.JSE_FLAG = 1;
                mJiaoSe.JSE_DESCRIPTION = txtJSE_MS.Text.Trim();
                bool isOK = dJiaoSe.Add(mJiaoSe);
                if (isOK)
                {
                    Response.Redirect("RoleManage.aspx");
                }
                else
                {
                    lblMsg.Text = "保存失败";
                }
            }
            catch (Exception ex)
            {
                lblMsg.Text = "错误信息：" + ex.Message;
            }
            #endregion
        }
        protected void btnEdit_Click(object sender, EventArgs e)
        {
            #region
            try
            {
                BDRoleBLL dJiaoSe = new BDRoleBLL();
                string strJSE_NAME = txtJSE_NAME.Text.Trim();
                if (strJSE_NAME == string.Empty)
                {
                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('请填写角色名称')", true);
                    return;
                }

                int iID = Convert.ToInt32(Request["id"].ToString());
                BDRoleModel mJiaoSe = new BDRoleModel();

                mJiaoSe.JSE_ID = iID;
                mJiaoSe.JSE_NAME = txtJSE_NAME.Text.Trim();
                mJiaoSe.JSE_FLAG = 1;
                mJiaoSe.JSE_DESCRIPTION = txtJSE_MS.Text.Trim();

                int iNum = dJiaoSe.GetRecordCount(" JSE_ID<>" + iID.ToString() + " AND JSE_NAME='" + mJiaoSe.JSE_NAME + "'");
                if (iNum > 0)
                {
                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('角色名称已经存在！')", true);
                    return;
                }
                bool isOK = dJiaoSe.Update(mJiaoSe);
                if (isOK)
                {
                    Response.Redirect("RoleManage.aspx");
                }
                else
                {
                    lblMsg.Text = "保存失败";
                }
            }
            catch (Exception ex)
            {
                lblMsg.Text = "错误信息：" + ex.Message;
            }
            #endregion
        }
    }
}